/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.basedata.category.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.dao.gen.BaseCategoryMapper;
import com.nmf.dao.gen.bean.BaseCategory;
import com.nmf.dao.gen.bean.BaseCategoryExample;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.IdGenerater;

/**
 * 基础分类Service
 * @author 北冥
 * @version 2018-12-28
 */
@Service
@Transactional(readOnly = true)
public class BaseCategoryService{

	@Autowired
	BaseCategoryMapper baseCategoryMapper;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	BaseBizCategoryService baseBizCategoryService;
	public BaseCategory get(String no) {
		BaseCategory baseCategory = baseCategoryMapper.selectByPrimaryKey(no);
		baseBizCategoryService.setParentCategory(baseCategory);
		return baseCategory;
	}
	
	public List<BaseCategory> findList(BaseCategory baseCategory) {
		BaseCategoryExample baseCategoryExample = new BaseCategoryExample();
		BaseCategoryExample.Criteria criteria = baseCategoryExample.createCriteria();
		
		List<BaseCategory> dlist =  baseCategoryMapper.selectByExample(baseCategoryExample);
		baseBizCategoryService.setParentCategory(dlist);
		return dlist;
	}
	
	public Page<BaseCategory> findPage(Page<BaseCategory> page, BaseCategory baseCategory) {
		BaseCategoryExample baseCategoryExample = new BaseCategoryExample();
		BaseCategoryExample.Criteria criteria = baseCategoryExample.createCriteria();
		
		baseCategoryExample.setPage(page);
		
		List<BaseCategory> dlist = baseCategoryMapper.selectByExample(baseCategoryExample);
		baseBizCategoryService.setParentCategory(dlist);
		page.setList(dlist);
		
		
		page.setCount(baseCategoryMapper.countByExample(baseCategoryExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(BaseCategory baseCategory) {
		baseBizCategoryService.clearParentCategory(baseCategory);
		baseBizCategoryService.setParentCategory(baseCategory);
		if(StringUtils.isNotEmpty(baseCategory.getId())&&get(baseCategory.getId())!=null) {
			baseCategoryMapper.updateByPrimaryKeySelective(baseCategory);
		}else {
			Date date = new Date();
			baseCategory.setCreateDate(date);
			baseCategory.setId(idGenerater.genClsNo("baseCategory"));
			baseCategory.setUpdateDate(date);
			baseCategoryMapper.insertSelective(baseCategory);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BaseCategory baseCategory) {
		baseCategoryMapper.deleteByPrimaryKey(baseCategory.getId());
	}
	
}